package com.tulingxueyuan.mall.modules.pms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.common.api.CommonPage;
import com.tulingxueyuan.mall.common.api.CommonResult;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductAttribute;
import com.tulingxueyuan.mall.modules.pms.model.PmsProductCategory;
import com.tulingxueyuan.mall.modules.pms.model.dto.RelationAttrInfoDTO;
import com.tulingxueyuan.mall.modules.pms.service.PmsProductAttributeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性参数表 前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;
    /**
     *   商品分类中商品属性的数据
     *   return request({
     *     url:'/productAttribute/list/'+cid,
     *     method:'get',
     *     params:params
     *   })
     */
    @GetMapping(value = "/list/{cid}")
    public CommonResult<CommonPage<PmsProductAttribute>> getList(@PathVariable long cid,
                                                                 @RequestParam(value = "type")  Integer type,
                                                                 @RequestParam(value = "pageNum",defaultValue  ="1")  Integer pageNum,
                                                                 @RequestParam(value = "pageSize",defaultValue  ="5")  Integer pageSize){

        Page page= pmsProductAttributeService.list(cid,type,pageNum,pageSize);

        return CommonResult.success(CommonPage.restPage(page));
    }
    /**
     *  属性添加
     *   return request({
     *     url:'/productAttribute/create',
     *     method:'post',
     *     data:data
     *   })
     */
    @ApiOperation("商品属性添加")
    @PostMapping (value = "/create")
    public CommonResult create(@RequestBody  PmsProductAttribute pmsProductAttribute) {
        boolean count = pmsProductAttributeService.create(pmsProductAttribute);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *   return request({
     *     url:'/productAttribute/'+id,
     *     method:'get'
     *   })
     */
    @ApiOperation("根据id查询商品属性单条记录")
    @GetMapping (value = "/{id}")
    @ResponseBody
    public CommonResult<PmsProductAttribute> getById(@PathVariable long id) {
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeService.getById(id);

        return CommonResult.success(pmsProductAttribute);
    }


    /**
     *   属性修改
     *   return request({
     *     url:'/productAttribute/update/'+id,
     *     method:'post',
     *     data:data
     *   })
     */
    @ApiOperation("商品属性添加")
    @PostMapping (value = "/update/{id}")
    public CommonResult update(@RequestBody  PmsProductAttribute pmsProductAttribute) {
        boolean count = pmsProductAttributeService.updateById(pmsProductAttribute);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *   删除商品属性
     *   return request({
     *     url:'/productAttribute/delete',
     *     method:'post',
     *     data:data
     *   })
     */
    @ApiOperation("商品属性的删除")
    @PostMapping (value = "/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        boolean count = pmsProductAttributeService.delete(ids);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }
    /**
     *  根据分类id获取关联的筛选属性
     *  url:'/productAttribute/attrInfo/'+productCategoryId,
     *     method:'get'
     */
    @GetMapping(value = "/attrInfo/{cId}")
    public CommonResult getRelationAttrInfoByCid(@PathVariable long cId){
       List<RelationAttrInfoDTO> list= pmsProductAttributeService.getRelationAttrInfoByCid(cId);

      return CommonResult.success(list);

    }


}

